package GUI;

import com.mysql.cj.jdbc.exceptions.MySQLQueryInterruptedException;
import 数据库和网络.myDatebase;
import 试题和试卷.*;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

public class questionWindow extends JFrame {

    public static void main(String[] args) {
        new questionWindow();
    }
    ArrayList<questionBaseItem> defList;
    static ArrayList<questionBaseItem> selList;
    static ArrayList<question> sel;
    ArrayList<questionBaseItem> fillList;
    ArrayList<questionBaseItem> subList;
    ArrayList<question> def;

    ArrayList<question> fill;
    ArrayList<question> sub;

    public static JPanel mainPanel;
    static int currentPage;
    static int page;
    static int size;
    JLabel num;
    JLabel paperLabel;
    static JLabel pageNumberLabel;
    static JLabel infoLabel;
    static JButton nextButton,preButton,addButton;
    static Font font = new Font("sanserif",Font.BOLD,10);
    Font pageNumberFont = new Font("sanserif",Font.BOLD,12);
    public questionWindow(){
        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif",Font.BOLD,24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu SelectiveMenu = new JMenu("Selective");
        JMenu DefinitionMenu = new JMenu("Definition");
        JMenu FillingBlankMenu = new JMenu("FillingBlank");
        JMenu SubjectiveMenu = new JMenu("Subjective");
        JMenuItem Selective = new JMenuItem("选择题");
        JMenuItem Definition = new JMenuItem("判断题");
        JMenuItem FillingBlank = new JMenuItem("填空题");
        JMenuItem Subjective = new JMenuItem("简答题");
        JMenu organizeMenu = new JMenu("organize");
        JMenuItem organize = new JMenuItem("组卷");
        JMenuItem randomOrganize = new JMenuItem("随机组卷");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem quit = new JMenuItem("退出");


        menuBar.add(SelectiveMenu);
        menuBar.add(DefinitionMenu);
        menuBar.add(FillingBlankMenu);
        menuBar.add(SubjectiveMenu);
        menuBar.add(organizeMenu);
        menuBar.add(quitMenu);

        Selective.addActionListener(new SelectiveListener());
        SelectiveMenu.add(Selective);
        Definition.addActionListener(new DefinitionListener());
        DefinitionMenu.add(Definition);
        FillingBlank.addActionListener(new FillingBlankListener());
        FillingBlankMenu.add(FillingBlank);
        Subjective.addActionListener(new SubjectiveListener());
        SubjectiveMenu.add(Subjective);
        organizeMenu.add(organize);
        organizeMenu.add(randomOrganize);
        organize.addActionListener(new organizeListener());
        randomOrganize.addActionListener(new randomOrganizeListener());
        quitMenu.add(quit);
        quit.addActionListener(new quitListener());


        mainPanel.setLayout(null);
        this.setTitle("试题库与组卷");
        this.setContentPane(mainPanel);
        this.setSize(980,700);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    public class quitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new teacherWindow("a");
            questionWindow.this.dispose();
        }
    }
    public class organizeListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new organizePaperWindow();
        }
    }
    public class randomOrganizeListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new randomParameterWindow();
        }
    }
    public class SelectiveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            mainPanel.removeAll();
            currentPage=1;
            myDatebase selQuery=new myDatebase("questioncollection");
            sel= new ArrayList<>();
            selList= new ArrayList<>();
            String sql="select * from questioncollection.selection_question";
            try {
                selQuery.statement.execute(sql);
                selQuery.resultSet=selQuery.statement.executeQuery(sql);

                while(selQuery.resultSet.next()){
                    String title=selQuery.resultSet.getString(2);
                    String text=selQuery.resultSet.getString(3);
                    Double dif=selQuery.resultSet.getDouble(4);
                    int full=selQuery.resultSet.getInt(5);
                    String a=selQuery.resultSet.getString(6);
                    String b=selQuery.resultSet.getString(7);
                    String c=selQuery.resultSet.getString(8);
                    String d=selQuery.resultSet.getString(9);
                    String answer= selQuery.resultSet.getString(10);
                    question curr=new selectionQuestion(title,text,"selection",dif,full,answer);
                    curr.setOption_a(a);
                    curr.setOption_b(b);
                    curr.setOption_c(c);
                    curr.setOption_d(d);

                    sel.add(curr);

                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            Iterator<question> ite=sel.iterator();
            int t=0;
            while(ite.hasNext()){
                question i=ite.next();
                ++t;
                selList.add(new selectionQuestionBaseItem(t,i));
                //add(selList.get(t-1));
                mainPanel.updateUI();
            }


            size = sel.size();
            page=size/5+1;

            if (page == 1) {
                for (int i = 1; i <= size; i++) {
                    add(selList.get(i-1));
                }
            } else if (page > 1) {
                for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                    add(selList.get(i));
                }

            }

            mainPanel.updateUI();


            displayFrame();

            if(page==1){
                mainPanel.remove(nextButton);
            }

            addButton.addActionListener(new addSelectionListener());
            infoLabel.setText("选择题题目数目："+sel.size());
            nextButton.removeAll();
            preButton.removeAll();
            nextButton.addActionListener(new selNextListener());
            preButton.addActionListener(new selPreListener());

        }
    }

        public class DefinitionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainPanel.removeAll();
                currentPage = 1;


                myDatebase selQuery = new myDatebase("questioncollection");
                def = new ArrayList<>();
                defList = new ArrayList<>();
                String sql = "select * from questioncollection.definition_question";
                try {
                    selQuery.statement.execute(sql);
                    selQuery.resultSet = selQuery.statement.executeQuery(sql);
                    while (selQuery.resultSet.next()) {
                        String title = selQuery.resultSet.getString(2);
                        String text = selQuery.resultSet.getString(3);
                        Double dif = selQuery.resultSet.getDouble(4);
                        int full = selQuery.resultSet.getInt(5);
                        String right = selQuery.resultSet.getString(6);
                        String wrong = selQuery.resultSet.getString(7);
                        String answer = selQuery.resultSet.getString(8);
                        question curr = new definitionQuestion(title, text, "definition", dif, full, answer);
                        curr.setRightOption(right);
                        curr.setWrongOption(wrong);

                        def.add(curr);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

                Iterator<question> ite = def.iterator();
                int t = 0;
                while (ite.hasNext()) {
                    question i = ite.next();
                    ++t;
                    defList.add(new definitionQuestionBaseItem(t, i));
                    //add(defList.get(t - 1));
                    //mainPanel.updateUI();
                }
                size = def.size();
                page=size/5+1;
                displayFrame();

                if (page == 1) {
                    for (int i = 0; i <size; i++) {
                        add(defList.get(i));
                    }
                } else if (page > 1) {
                    for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                        add(defList.get(i));
                    }
                }

                if(page==1){
                    mainPanel.remove(nextButton);
                }

                nextButton.addActionListener(new defNextListener());
                preButton.addActionListener(new defPreListener());
                infoLabel.setText("判断题题目数目：" + def.size());
                addButton.addActionListener(new addDefinitionListener());
                mainPanel.updateUI();



            }
        }

        public class FillingBlankListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainPanel.removeAll();
                currentPage=1;
                fill= new ArrayList<>();
                fillList= new ArrayList<>();
                myDatebase selQuery=new myDatebase("questioncollection");
                String sql="select * from questioncollection.fillingblank_question";
                try {
                    selQuery.statement.execute(sql);
                    selQuery.resultSet=selQuery.statement.executeQuery(sql);
                    while(selQuery.resultSet.next()){
                        String title=selQuery.resultSet.getString(2);
                        String text=selQuery.resultSet.getString(3);
                        Double dif=selQuery.resultSet.getDouble(4);
                        int full=selQuery.resultSet.getInt(5);
                        String answer=selQuery.resultSet.getString(6);
                        question curr=new fillingblankQuestion(title,text,"fillingblank",dif,full,answer);
                        fill.add(curr);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

                Iterator<question> ite=fill.iterator();
                int t=0;
                while(ite.hasNext()){
                    question i=ite.next();
                    ++t;
                    fillList.add(new fillingblankQuestionBaseItem(t,i));
                }
                size = fill.size();
                page=size/5+1;

                if (page == 1) {
                    for (int i = 0; i <size; i++) {
                        add(fillList.get(i));
                    }
                } else if (page > 1) {
                    for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                        add(fillList.get(i));
                    }

                }

                mainPanel.updateUI();


                displayFrame();

                if(page==1){
                    mainPanel.remove(nextButton);
                }

                infoLabel.setText("填空题题目数目："+fill.size());
                mainPanel.updateUI();
                addButton.addActionListener(new addFillingBlankListener());
                nextButton.removeAll();
                preButton.removeAll();
                nextButton.addActionListener(new fillNextListener());
                preButton.addActionListener(new fillPreListener());
            }
        }

        public class SubjectiveListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainPanel.removeAll();
                currentPage=1;
                sub= new ArrayList<>();
                subList= new ArrayList<>();
                myDatebase selQuery=new myDatebase("questioncollection");
                String sql="select * from questioncollection.subjective_question";
                try {
                    selQuery.statement.execute(sql);
                    selQuery.resultSet=selQuery.statement.executeQuery(sql);
                    while(selQuery.resultSet.next()){
                        String title=selQuery.resultSet.getString(2);
                        String text=selQuery.resultSet.getString(3);
                        Double dif=selQuery.resultSet.getDouble(4);
                        int full=selQuery.resultSet.getInt(5);
                        String answer=selQuery.resultSet.getString(6);
                        question curr=new subjectiveQuestion(title,text,"subjective",dif,full,answer);
                        sub.add(curr);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

                Iterator<question> ite=sub.iterator();
                int t=0;
                while(ite.hasNext()){
                    question i=ite.next();
                    t++;
                    subList.add(new subjectiveQuestionBaseItem(t,i));
                }
                size = sub.size();
                page=size/5+1;

                if (page == 1) {
                    for (int i = 0; i < size; i++) {
                        add(subList.get(i));
                    }
                } else if (page > 1) {
                    for (int i = (currentPage - 1) * 5; i <= currentPage * 5 - 1; i++) {
                        add(subList.get(i));
                    }

                }

                mainPanel.updateUI();
                displayFrame();

                if(page==1){
                    mainPanel.remove(nextButton);
                }

                infoLabel.setText("简答题题目数目："+sub.size());
                mainPanel.updateUI();
                addButton.addActionListener(new addSubjectiveListener());
                nextButton.removeAll();
                preButton.removeAll();
                nextButton.addActionListener(new subNextListener());
                preButton.addActionListener(new subPreListener());
            }
        }

        public  class addSelectionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                new addSelectionWindow();

            }
        }

        public class addDefinitionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                new addDefinitionWindow();

            }
        }

        public class addFillingBlankListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                new addFillingBlankWindow();

            }
        }

        public class addSubjectiveListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                new addSubjectiveWindow();

            }
        }
    public class selNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(selList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(selList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(selList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class selPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(selList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(selList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class defNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(defList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(defList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(defList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class defPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(defList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(defList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class fillNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(fillList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(fillList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(fillList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class fillPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(fillList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(fillList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public class subNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(subList.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(subList.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(subList.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class subPreListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(subList.get(i));
            }


            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(subList.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }

        public  void setTitleType(JLabel label) {
            Font bigFont = new Font("sanserif", Font.PLAIN, 16);
            label.setBackground(Color.lightGray);
            label.setOpaque(true);
            label.setFont(bigFont);
            label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
            label.setHorizontalAlignment(SwingConstants.CENTER);
        }

        public void setTextType(JLabel label) {
            Font smallFont = new Font("sanserif", Font.PLAIN, 12);
            label.setBackground(Color.white);
            label.setOpaque(true);
            label.setFont(smallFont);
            label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
            label.setHorizontalAlignment(SwingConstants.CENTER);
        }

        public void setButtonType(JButton button) {
            Font smallFont = new Font("sanserif", Font.PLAIN, 14);
            button.setBackground(Color.GRAY);
            button.setFont(smallFont);
            button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
            button.setHorizontalAlignment(SwingConstants.CENTER);
        }

        public void displayFrame() {
            infoLabel = new JLabel("题目数目:");
            JLabel numLabel = new JLabel("序号");
            JLabel paperInfoLabel = new JLabel("题目信息");
            JLabel paperDetailLabel = new JLabel("修改");
            JLabel paperDoLabel = new JLabel("删除");

            preButton = new JButton("上一页");

            preButton.setBounds(750, 450, 70, 30);
            preButton.setFont(font);
            preButton.setBackground(Color.lightGray);

            addButton = new JButton("添加试题");
            //overallButton.addActionListener(new studentRank.overallListener());
            addButton.setBounds(810, 50, 90, 40);
            //overallButton.setFont(font);
            addButton.setBackground(Color.lightGray);


            setTitleType(numLabel);
            setTitleType(paperInfoLabel);
            setTitleType(paperDetailLabel);
            setTitleType(paperDoLabel);

            JButton backButton = new JButton("返回");

            mainPanel.setLayout(null);

            backButton.setBackground(Color.lightGray);

            infoLabel.setBounds(80, 30, 200, 40);
            numLabel.setBounds(80, 120, 60, 50);
            paperInfoLabel.setBounds(140, 120, 600, 50);
            paperDetailLabel.setBounds(740, 120, 80, 50);
            paperDoLabel.setBounds(820, 120, 80, 50);

            backButton.setBounds(820, 530, 80, 30);
            backButton.addActionListener(new backListener());

            mainPanel.add(paperDetailLabel);
            mainPanel.add(paperDoLabel);
            mainPanel.add(numLabel);
            mainPanel.add(infoLabel);
            mainPanel.add(paperInfoLabel);
            mainPanel.add(backButton);
            mainPanel.add(addButton);


            //size =paperNumber;

            page = (size - 1) / 5 + 1;
            currentPage = 1;

            pageNumberLabel = new JLabel(currentPage + "/" + page);
            mainPanel.add(pageNumberLabel);
            // pageNumberLabel.setFont(pageNumberFont);
            pageNumberLabel.setBounds(430, 480, 80, 30);


            nextButton = new JButton("下一页");

            nextButton.setBounds(830, 480, 70, 30);
            nextButton.setFont(font);
            nextButton.setBackground(Color.lightGray);
            mainPanel.add(nextButton);


            //backButton.addActionListener(new studentRank.backListener());
            infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
            infoLabel.setVerticalAlignment(SwingConstants.CENTER);
            this.setSize(980, 700);
            mainPanel.setSize(this.getSize());
            mainPanel.setBackground(Color.white);
            this.setBackground(Color.white);
            this.setContentPane(mainPanel);
            this.setLocationRelativeTo(null);//居中
            this.setTitle("学生在线考试平台");
            this.setVisible(true);
            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public  void add(questionBaseItem item) {
            mainPanel.add(item.num);
            mainPanel.add(item.paperLabel);
            mainPanel.add(item.doButton);
            mainPanel.add(item.detailButton);
        }
    public void remove(questionBaseItem item) {
        mainPanel.remove(item.num);
        mainPanel.remove(item.paperLabel);
        mainPanel.remove(item.doButton);
        mainPanel.remove(item.detailButton);
    }

    public class backListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            questionWindow.this.dispose();
            new teacherWindow("a");
        }
    }
    }

